//
//  CartoonClassifyMainViewController.m
//  Double
//
//  Created by 孟令博 on 15/5/25.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "CartoonClassifyMainViewController.h"
#import "CartoonClassifyNewViewController.h"
#import "CartoonClassifyHotViewController.h"
#import "CartoonClassifyGoodViewController.h"

@interface CartoonClassifyMainViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) UISegmentedControl *segment;
@property (nonatomic, retain) UIScrollView *scroll;
@end

@implementation CartoonClassifyMainViewController
- (void)dealloc
{
    [_cartoonGood release];
    [_cartoonHot release];
    [_cartoonNew release];
    [_segment release];
    [_scroll release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //        self.view.backgroundColor = [UIColor orangeColor];
        self.cartoonNew = [[CartoonClassifyNewViewController alloc] init];
        self.cartoonHot = [[CartoonClassifyHotViewController alloc] init];
        self.cartoonGood = [[CartoonClassifyGoodViewController alloc] init];

    }
    return self;
}
- (void)loadView
{
    [super loadView];
    [self createScrollView];
    [self createSegment];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.titleView = _segment;
   
}

#pragma mark -
#pragma mark 创建scrollView
- (void)createScrollView
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
    self.scroll.pagingEnabled = YES;
    self.scroll.delegate = self;
    //最新页面偏移量
    self.cartoonNew.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [self addChildViewController:self.cartoonNew];
    //最热页面偏移量
    
    self.cartoonHot.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    [self addChildViewController:self.cartoonHot];
    //评分页面偏移量
    
    self.cartoonGood.view.frame = CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:self.cartoonGood];
    
    [self.scroll addSubview:self.cartoonNew.view];
    [self.scroll addSubview:self.cartoonHot.view];
    [self.scroll addSubview:self.cartoonGood.view];
    
    
    [self.view addSubview:self.scroll];
    
    [self.scroll release];
}

#pragma mark -
#pragma mark 创建segmentControl
- (void)createSegment
{
    NSArray *array = [NSArray arrayWithObjects:@"推荐",@"片单",@"分类", nil];
    self.segment = [[UISegmentedControl alloc] initWithItems:array];
    self.segment.frame = CGRectMake(40, 0, self.view.frame.size.width / 2.5, 38);
    self.segment.selectedSegmentIndex = 0;
    self.segment.layer.cornerRadius = 20;
    
    [self.segment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
    
    
}

#pragma mark -
#pragma mark scrollView协议
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        self.segment.selectedSegmentIndex = 0;
    } else if (scrollView.contentOffset.x == self.scroll.frame.size.width){
        self.segment.selectedSegmentIndex = 1;
    } else if (scrollView.contentOffset.x == self.scroll.frame.size.width * 2){
        self.segment.selectedSegmentIndex = 2;
    }
}



#pragma mark -
#pragma mark segmentControl 点击事件
- (void)segmentChanged:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.scroll.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width, 0);
            break;
        case 2:
            self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width * 2, 0);
            break;
        default:
            break;
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
